/*
 ----------------------------------------------------------------------------------
                           "THE BEER-WARE COPYRIGHT LICENSE"
                                     (Revision 43)
                                      -----------
    Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
    this file has been sent directly to You by the author, You can do whatever You
    want with this stuff as long as You don't sell or rent it. If we meet some day,
    and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                 __________
                                                                 Paweł Rymaszewski
  ----------------------------------------------------------------------------------
 */

package processing.objects;

import constants.UiConstants;
import utils.properties.Configuration;

import java.io.File;
import java.text.ParseException;
import java.util.Date;

/**
 * Class representing instance of Search Result.
 *
 * @author rymek
 *         User: rymek
 *         Date: 30.06.11
 *         Time: 10:10
 */
public class Result {

    private Date msgRcvDttm;            // result time received
    private Date msgRspDttm;            // result response posted (only synchronous messages)
    private int processingTime;         // result processing time
    private String id;                  // result identifier (guid - sync messages, pubid - async messages)
    private File xmlResultFile;         // result file path for messageInfo body
    private int logtype;                // result logtype: 1 - request, 3 - response
    private String status;              // result publication status
    private Message messageInfo;        // complex info about this result
    private String xmlString;           // result body as string

    /**
     * Default constructor
     */
    public Result() {
    }

    /**
     * Copy contructor
     *
     * @param result Result to be copied
     */
    public Result(Result result) {
        this.id = result.id;
        this.msgRcvDttm = result.msgRcvDttm;
        this.msgRspDttm = result.msgRspDttm;
        this.processingTime = result.processingTime;
        this.xmlResultFile = result.xmlResultFile;
        this.logtype = result.logtype;
        this.status = result.getStatus();
        this.messageInfo = result.messageInfo;
        this.xmlString = result.xmlString;
    }

    /**
     * Creates empty request from input Result object
     *
     * @param result input Result
     * @return empty request
     */
    public static Result createEmptyRequest(Result result) {
        Result emptyRequest = new Result(result);
        emptyRequest.setLogtype(1);
        emptyRequest.setXmlString(UiConstants.EMPTY_MESSAGE_STRING);

        return emptyRequest;
    }

    /**
     * Creates empty response from input Result object
     *
     * @param result input Result
     * @return empty response
     */
    public static Result createEmptyResponse(Result result) {

        Result emptyResponse = new Result(result);
        emptyResponse.setLogtype(3);
        emptyResponse.setXmlString(UiConstants.EMPTY_MESSAGE_STRING);

        return emptyResponse;
    }

    /**
     * Method adds header to XML string messageInfo
     *
     * @param header messageInfo header to be applied to messageInfo body
     */
    public void applyHeader(String header) {
        this.xmlString = header + xmlString;
    }

    public File getXmlResultFile() {
        return xmlResultFile;
    }

    public void setXmlResultFile(File xmlResultFile) {
        this.xmlResultFile = xmlResultFile;
    }

    public Date getMsgRcvDttm() {
        return msgRcvDttm;
    }

    public void setMsgRcvDttm(Date msgRcvDttm) throws ParseException {
        this.msgRcvDttm = Configuration.dttmFormat.parse(Configuration.dttmFormat.format(msgRcvDttm));
    }

    public Date getMsgRspDttm() {
        return msgRspDttm;
    }

    public void setMsgRspDttm(Date msgRspDttm) throws ParseException {
        this.msgRspDttm = Configuration.dttmFormat.parse(Configuration.dttmFormat.format(msgRspDttm));
    }

    public int getProcessingTime() {
        return processingTime;
    }

    public void setProcessingTime(int processingTime) {
        this.processingTime = processingTime;
    }

    public String getId() {
        return id;
    }

    public void setId(String guid) {
        this.id = guid;
    }

    public String getXmlString() {
        return xmlString;
    }

    public void setXmlString(String xmlString) {
        this.xmlString = xmlString;
    }

    public Integer getLogtype() {
        return logtype;
    }

    public void setLogtype(int logtype) {
        this.logtype = logtype;
    }

    public Message getMessageInfo() {
        return messageInfo;
    }

    public void setMessageInfo(Message messageInfo) {
        this.messageInfo = messageInfo;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Result{" +
                "msgRcvDttm=" + msgRcvDttm +
                ", msgRspDttm=" + msgRspDttm +
                ", processingTime=" + processingTime +
                ", id='" + id + '\'' +
                ", xmlResultFile=" + xmlResultFile +
                ", logtype=" + logtype +
                ", status='" + status + '\'' +
                ", messageInfo=" + messageInfo +
                ", xmlString='" + xmlString + '\'' +
                '}';
    }


}
